package com.hxh.wechat.adpter

import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.consumer.ShuttersConsumer
import com.hxh.wechat.R
import com.hxh.wechat.activity.ChatPageActivity
import com.hxh.wechat.activity.PhotoViewActivity
import com.hxh.wechat.constant.GlobalConstant
import com.hxh.wechat.model.MsgType
import com.hxh.wechat.util.EmoJiUtils


class ChatMessageAdapter(private val chatMessageList: List<ChatPageActivity.ChatMessage>) :
    RecyclerView.Adapter<ChatMessageAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.textView)
        val imageView: ImageView = view.findViewById(R.id.imageView)
        val chatTime: TextView = view.findViewById(R.id.chatTime)
        val textImageView: ImageView = view.findViewById(R.id.textImgView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(viewType, parent, false)
        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ChatMessageAdapter.ViewHolder, position: Int) {

        val chatMessage = chatMessageList[position]
        val type = chatMessage.type
        var content = chatMessage.content
        if (type == MsgType.TEXT.type) {
            holder.textImageView.visibility = View.GONE
            holder.textView.text =
                EmoJiUtils.parseEmoJi(1, holder.textView.context, content)
            holder.textView.visibility = View.VISIBLE
        } else if (type == MsgType.IMAGE.type) {
            holder.textView.visibility = View.GONE
            //判断是自己的服务端的图片数据还是别人的图片数据
            var urlImg = content;
            if (!content.contains("http")) {
                urlImg = GlobalConstant.IMAGES_URL + urlImg;
            }
            //展示图片
            holder.textImageView.setOnClickListener {
                val intent = Intent(holder.textView.context, PhotoViewActivity::class.java)
                val filteredMessages = chatMessageList.filter { it.type == 2 }
                val contentArray: Array<String> =
                    filteredMessages.map { it.content }.toTypedArray()
                var indexOf = contentArray.indexOf(urlImg)
                intent.putExtra("imageUrlList", contentArray)
                intent.putExtra("currentPosition", indexOf)
                holder.textView.context.startActivity(intent)
            }
            holder.textImageView.load(urlImg)
            holder.textImageView.visibility = View.VISIBLE
        }
        holder.chatTime.text = chatMessage.time.toLocaleString()
        holder.imageView.load(chatMessage.targetImage);
    }

    //有两种行Layout，所以重写此方法
    override fun getItemViewType(position: Int): Int {
        val message = chatMessageList[position]
        return if (message.isMe) {
            //如果是我的，靠右显示
            R.layout.chat_message_right_item
        } else {
            //对方的，靠左显示
            R.layout.chat_message_left_item
        }
    }

    override fun getItemCount() = chatMessageList.size
}